# pull from devel image instead of base
FROM registry.gitee-ai.local/base/iluvatar-corex:3.2.0-bi100

# Set bash as the default shell
ENV SHELL=/bin/bash

# Create a working directory
WORKDIR /app/

ENV DEBIAN_FRONTEND=noninteractive

# Replace the default update source with a mirror site URL for China
RUN sed -i 's@/archive.ubuntu.com/@/mirrors.aliyun.com/@g' /etc/apt/sources.list
RUN apt-get clean

# Build with some basic utilities
RUN apt-get update && apt-get install -y software-properties-common
RUN add-apt-repository ppa:rabbitmq/rabbitmq-erlang -y && \
    apt-get install -y \
    erlang-inets erlang-runtime-tools erlang-ssl erlang-parsetools \
    elixir erlang-dev erlang-os-mon erlang-xmerl build-essential \
    apt-utils \
    vim wget\
    git

ENV LIVEBOOK_APP_SERVICE_NAME "🐳 Gitee AI - $SPACE_TITLE"
ENV LIVEBOOK_APP_SERVICE_URL "https://ai.gitee.com/apps/$SPACE_AUTHOR_NAME/$SPACE_REPO_NAME"
ENV LIVEBOOK_UPDATE_INSTRUCTIONS_URL "https://livebook.dev"
ENV LIVEBOOK_WITHIN_IFRAME "true"
ENV LIVEBOOK_DATA_PATH "/app/data"
ENV LIVEBOOK_APPS_PATH_WARMUP "manual"
ENV LIVEBOOK_HOME "/home/user"
ENV LIVEBOOK_PORT 7860

ENV MIX_ENV="prod"
ENV MIX_HOME="/app/.mix"
ENV MIX_INSTALL_DIR="/app/.mix"
ENV BUMBLEBEE_CACHE_DIR="/app/.bumblebee"

EXPOSE 7860

USER root

RUN mkdir -p /app/.mix
RUN mkdir -p /app/.bumblebee
RUN mkdir -p /home/user
RUN chmod 777 /home/user
RUN mkdir -p /app/data
RUN chmod 777 /app/data

# RUN mix local.hex --force
# RUN mix do local.rebar --force
RUN wget https://mirrors.gitee.com/gitee-mirrors/other/rebar && chmod +x rebar && mv rebar /app/.mix/
RUN wget https://mirrors.gitee.com/gitee-mirrors/other/rebar3 && chmod +x rebar3 && mv rebar3 /app/.mix/
ENV HEX_HTTP_TIMEOUT=1200
ENV HEX_HTTP_CONCURRENCY=4
ENV HTTP_PROXY "$HTTP_PROXY"
ENV HTTPS_PROXY "$HTTP_PROXY"

RUN wget https://mirrors.gitee.com/gitee-mirrors/other/bore && chmod +x bore

RUN mix archive.install git https://gitee.com/edmondfrank/hex --force
RUN mix local.rebar rebar3 /app/.mix/rebar3 --force
RUN mix escript.install git https://gitee.com/mirrors_livebook-dev/livebook tag v0.12.1 --force

ENV PATH="$PATH:/app/.mix/escripts"

COPY ./start.sh .

# ENTRYPOINT ["./start.sh"]

# start livebook book
CMD ["livebook", "server", "--ip", "0.0.0.0", "-p", "7860"]
